package main

import (
	"encoding/json"
	"fmt"
	"go-tour/pkg/utils"
)

type Social struct {
	Facebook string `json:"facebook"`
	Twitter  string `json:"twitter"`
}

type User struct {
	Name   string `json:"name"`
	Type   string `json:"type"`
	Age    int    `json:"age"`
	Social Social `json:"social"`
}

type Users struct {
	Users []User
}

func main() {
	var (
		users Users
		err   error
	)
	if users, err = GetUsers("./users.json"); err == nil {
		PrintUsers(users)
	} else {
		fmt.Println("error:", err)
	}

}

func GetUsers(jsonFilePath string) (users Users, err error) {
	var (
		result string
	)
	result, err = utils.ReadJsonFileWithSearch(jsonFilePath, 4)
	if err == nil && result != "" {
		fmt.Println("Successfully opened " + jsonFilePath)
		err = json.Unmarshal([]byte(result), &users)
	}
	return
}

func PrintUsers(users Users) {
	if users.Users == nil || len(users.Users) == 0 {
		return
	}
	for _, user := range users.Users {
		fmt.Println("User Name:", user.Name)
		fmt.Println("User Type:", user.Type)
		fmt.Println("User Age:", user.Age)
		fmt.Println("Facebook Url:", user.Social.Facebook)
	}
	fmt.Print(users.Users)
}
